 package cn.jh.smartos

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.Window

/**
 * Created by imac on 2017/5/4.
 */

class CommDialog : Dialog {
    private var mDialogRootView: View? = null

    constructor(context: Context, themeResId: Int) : super(context, themeResId)

    constructor(context: Context) : super(context) 

    fun apply(p: Builder.CommParam) {
        if (p.mView != null) {
            mDialogRootView = p.mView
        }
        if (p.mViewLayoutResId != 0) {
            mDialogRootView = View.inflate(p.context, p.mViewLayoutResId, null)
        }
        /**
         * 设置内容
         */
        setContentView(mDialogRootView!!)
        /**
         * 设置动画
         */
//        val window = window
        if (p.animation != 0) {
            window.setWindowAnimations(p.animation)
        }
        window!!.setGravity(p.gravity)
        //        设置 宽高
        window.setLayout(p.width, p.height)
    }

    /**
     * 设置点击监听
     */
    fun setOnClickListener(ViewId: Int, listener: View.OnClickListener) {
        mDialogRootView!!.findViewById<View>(ViewId).setOnClickListener(listener)
    }

    /**
     * 获取view
     */
    fun <T : View> getView(viewId: Int): T {
        return mDialogRootView?.findViewById<View>(viewId) as T
    }

    class Builder {

        private var P: CommParam? = null

        constructor(context: Context) {
            P = CommParam(context)
        }

        constructor(context: Context, themeResId: Int) {
            P = CommParam(context, themeResId)
        }

        fun setView(view: View): Builder {
            P?.mView = view
            P?.mViewLayoutResId = 0
            return this
        }

        fun setView(layoutId: Int): Builder {
            P?.mView = null
            P?.mViewLayoutResId = layoutId
            return this
        }

        fun creat(): CommDialog {
            val commDialog = CommDialog(P!!.context, P!!.themeResId)
            commDialog.apply(P!!)
            return commDialog
        }

        /*
        从底部弹出
         */
        fun fromBottom(): Builder {
            P!!.animation = 0
            P!!.gravity = Gravity.BOTTOM
            return this
        }

        //        设置宽度
        fun setWidth(width: Int): Builder {
            P!!.width = width
            return this
        }

        //        设置高度
        fun setHeight(height: Int): Builder {
            P!!.height = height
            return this
        }

        /*
               设置动画
                */
        fun setAnimations(animations: Int): Builder {
            P!!.animation = animations
            return this
        }

        inner class CommParam {
            var context: Context//上下文
            var mView: View? = null
            var mViewLayoutResId = 0
            var themeResId = 0//主题
            var width = ViewGroup.LayoutParams.MATCH_PARENT
            var height = ViewGroup.LayoutParams.WRAP_CONTENT
            var animation = 0//动画
            var gravity = Gravity.CENTER

            constructor(context: Context) {
                this.context = context
            }

            constructor(context: Context, themeResId: Int) {
                this.context = context
                this.themeResId = themeResId

            }
        }
    }
}
